import Vue from 'vue'
import Vuex from 'vuex'
import zutil from '@/util/util'
import { zloginInfo } from '@/util/api';

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		storecolor: ['#325B69', '#698570', '#AE5548', '#6D9EA8', '#9CC2B0', '#C98769'],
		storeAjaxData: null,
		hasLogin: false,
		loginUserInfo: null,
		hospitalUserInfo: null,
		myPatientFlag: false,
		myPatientFlagS: false,
		hospitalJump: false,
		tempPatienInfo: null,
		zuiOrgTree: null,
		dashboardData: null
	},
	mutations: {
		setStoreAjaxData (state, data){
			state.storeAjaxData = data;
		},
		setLogin (state, data) {
			state.hasLogin = data;
		},
		setLoginUserInfo (state, data) {
			state.loginUserInfo = data;
			zutil.lStorage.setParam('loginUserInfo', JSON.stringify(data));
		},
		setHospitalUserInfo (state, data) {
			state.hospitalUserInfo = data;
			zutil.lStorage.setParam('hospitalUserInfo', JSON.stringify(data));
		},
		setHospitalJump (state, data) {
			state.hospitalJump = data;
		},
		setTempPatienInfo (state, data) {
			state.tempPatienInfo = data;
		},
		setMyPatientFlag (state, data) {
			state.myPatientFlag = data;
		},
		setMyPatientFlagS (state, data) {
			state.myPatientFlagS = data;
		},
		setOrgTree (state, data) {
			state.zuiOrgTree = data;
			zutil.lStorage.setParam('zuiOrgTree', JSON.stringify(data));
		},
		setDashboardData (state, data) {
			state.dashboardData = {...data};
			zutil.lStorage.setParam('dashboardData', JSON.stringify(data));
		}
	},
	actions: {
		getPermissionList: async ({ state, commit }) => {
      // 这里是为了防止重复获取
      if (state.loginUserInfo) return;
      
      // zutil.lStorage.removeParam('loginUserInfo');
      let loginUserInfo = zutil.lStorage.getParam('loginUserInfo');
      if(loginUserInfo){
      	loginUserInfo = JSON.parse(loginUserInfo);
      	commit('setLoginUserInfo', loginUserInfo);
      }else{
      	let res = await zloginInfo();
      	res = zutil.formatAxiosData(res);
      	commit('setLoginUserInfo', res.data);
      }
    },
    setDashboardDataAction: ({state, commit}, data) => {
    	commit('setDashboardData', data);
    }
	}
})

export default store
